package com.hnkj.caiyunweather.ui.place

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.hnkj.caiyunweather.MainActivity
import com.hnkj.caiyunweather.databinding.FragmentPlaceBinding
import com.hnkj.caiyunweather.ui.weather.WeatherActivity

class PlaceFragment : Fragment() {

    //    private val viewmodel:PlaceViewModel by ViewModels()
    val viewmodel by lazy {
        ViewModelProviders.of(this).get(PlaceViewModel::class.java)
    }
    lateinit var fragmentPlaceBinding: FragmentPlaceBinding
    lateinit var placeAdaper: PlaceAdapter
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        fragmentPlaceBinding = FragmentPlaceBinding.inflate(inflater, container, false)
        return fragmentPlaceBinding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        if(activity is MainActivity && viewmodel.isPlaceSaved()){
            val place = viewmodel.getSavedPlace()
            val intent = Intent(context,WeatherActivity::class.java).apply {
                putExtra("location_lng",place.location.lng)
                putExtra("location_lat",place.location.lat)
                putExtra("place_name",place.name)
            }
            startActivity(intent)
            activity?.finish()
            return
        }

        val layoutManager = LinearLayoutManager(activity)
        fragmentPlaceBinding.recycleView.layoutManager = layoutManager
        placeAdaper = PlaceAdapter(this, viewmodel.placeList)
        fragmentPlaceBinding.recycleView.adapter = placeAdaper
        fragmentPlaceBinding.searchPlaceEdit.addTextChangedListener {
            val content = it.toString()
            if (content.isNotEmpty()) {
                viewmodel.searchPlaces(content)
            } else {
                fragmentPlaceBinding.recycleView.visibility = View.GONE
                fragmentPlaceBinding.ivBg.visibility = View.VISIBLE
                viewmodel.placeList.clear()
                placeAdaper.notifyDataSetChanged()
            }
        }
        viewmodel.placeLiveData.observe(viewLifecycleOwner, Observer {
            val places = it.getOrNull()
            if (places != null) {
                fragmentPlaceBinding.recycleView.visibility = View.VISIBLE
                fragmentPlaceBinding.ivBg.visibility = View.GONE
                viewmodel.placeList.clear()
                viewmodel.placeList.addAll(places)
                placeAdaper.notifyDataSetChanged()
            } else {
                Toast.makeText(requireContext(), "未能查询到任何地点", Toast.LENGTH_SHORT).show()
                it.exceptionOrNull()?.printStackTrace()
            }
        })
    }


}